#!/bin/bash

upper_dir='/basic'
base_dir="$upper_dir"/srs.oschina

# 依赖FFmpeg、redhat-lsb，下载要用到Git

install_ffmpeg() {
  echo '要先安装FFmpeg'
}

dl_lsb() {
  yum install -y redhat-lsb
}

makeDir() {
  if [ ! -d $upper_dir ]; then
    mkdir -p "$upper_dir"
  fi
}

download() {
  cd $upper_dir || exit
  git clone http://git.oschina.net/winlinvip/srs.oschina
  if [ ! -d $base_dir ]; then
    echo '下载失败！'
    exit
  fi
}

install() {
  cd "$base_dir"/trunk || exit
  ./configure && make
}

#默认配置（4.0.117版本）要占1985 8080 8000端口，8080是最敏感的
editPort() {
  local conf="$base_dir"/trunk/conf/srs.conf
  line1=$(sed -nr '/listen          8080;/=' $conf)
  sed -i "${line1}c listen          8848;" $conf
}

finishTouch() {
  firewall-cmd --zone=public --add-port=1935/tcp --permanent
  firewall-cmd --zone=public --add-port=8848/tcp --permanent
  #注意是udp
  firewall-cmd --zone=public --add-port=8000/udp --permanent

  if [ ! -d /link ]; then
    mkdir "/link"
  fi
  ln -s /basic/srs.oschina/trunk/etc/init.d/srs /link/srs
  /link/srs start
  /link/srs status
}

dl_lsb
makeDir
download
install
editPort
finishTouch
